package cn.test.auto;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class auto {

	private static String mk = "sys";

	// 当前类名称
	private static String classPath = "cn.test.auto";

	private static String entityName = "SysMenu";

	private static String entityPath = "cn.always.xiajia.admin." + mk + ".entity";

	private static String mapperPath = "cn.always.xiajia.admin." + mk + ".mapper";

	private static String servicePath = "cn.always.xiajia.admin." + mk + ".service";

	private static String serviceImplPath = "cn.always.xiajia.admin." + mk + ".service.impl";

	public static void main(String[] args) {
		String[] ars = new String[] { "OAuth2AccessToken", "OAuth2Client", "OAuth2RefreshToken" };
		List<String> s = new ArrayList<>();
		List<String> e = new ArrayList<>();
		for (String ar : ars) {
			entityName = ar;
			Boolean boolean1 = autoJava();
			if (boolean1) {
				s.add(ar);
			} else {
				e.add(ar);
			}
		}
		log.info("预生成java文件----" + Arrays.asList(ars).toString());
		log.info("成功生成java文件----" + s.toString());
		log.info("失败成java文件----" + e.toString());
	}

	public static Boolean autoJava() {
		String firstChar = entityName.substring(0, 1).toLowerCase();
		String restOfString = entityName.substring(1);
		String entityNameXx = firstChar + restOfString;
		List<String> list = Arrays.asList(new String[] { "mapper", "service", "serviceImpl" });
		String path = System.getProperty("user.dir");
		log.info("---path-------" + path);
		Boolean flag = true;
		File fileMappercj = null;
		File fileServicecj = null;
		File fileServiceImplcj = null;
		for (String s : list) {
			if ("mapper".equals(s)) {
				String fileMapperS = path + "\\src\\main\\java\\" + mapperPath.replace(".", "\\") + "\\" + entityName + "Mapper.java";
				log.info("---fileMapperS-------" + fileMapperS);
				fileMappercj = new File(fileMapperS);
				if (fileMappercj.exists()) {
					log.info("--mapper文件存在-------");
					flag = false;
					break;
				} else {
					log.info("--mapper文件不存在-------");
				}
			} else if ("service".equals(s)) {
				String fileServiceS = path + "\\src\\main\\java\\" + servicePath.replace(".", "\\") + "\\" + entityName + "Service.java";
				log.info("---fileMapperS-------" + fileServiceS);
				fileServicecj = new File(fileServiceS);
				if (fileServicecj.exists()) {
					log.info("--service文件存在-------");
					flag = false;
					break;
				} else {
					log.info("--service文件不存在-------");
				}
			} else if ("serviceImpl".equals(s)) {
				String fileServiceImplS = path + "\\src\\main\\java\\" + serviceImplPath.replace(".", "\\") + "\\" + entityName
						+ "ServiceImpl.java";
				log.info("---fileMapperS-------" + fileServiceImplS);
				fileServiceImplcj = new File(fileServiceImplS);
				if (fileServiceImplcj.exists()) {
					log.info("--serviceImpl文件存在-------");
					flag = false;
					break;
				} else {
					log.info("--serviceImpl文件不存在-------");
				}
			}
		}

		if (flag) {
			for (String s : list) {
				if ("mapper".equals(s)) {
					String fileMapperLyS = path + "\\src\\test\\java\\" + classPath.replace(".", "\\") + "\\SysUserMapper.txt";
					log.info("--fileMapperLyS-------" + fileMapperLyS);
					File fileMapperLy = new File(fileMapperLyS);
					String mapperLy = read(fileMapperLy);
					// log.info("--mapperLy-------" + mapperLy);
					mapperLy = mapperLy.replaceAll("\\{entityName\\}", entityName);
					mapperLy = mapperLy.replaceAll("\\{entityNameXx\\}", entityNameXx);
					mapperLy = mapperLy.replaceAll("\\{entityPath\\}", entityPath);
					mapperLy = mapperLy.replaceAll("\\{mapperPath\\}", mapperPath);
					mapperLy = mapperLy.replaceAll("\\{servicePath\\}", servicePath);
					mapperLy = mapperLy.replaceAll("\\{serviceImplPath\\}", serviceImplPath);
					write(mapperLy, fileMappercj);
				} else if ("service".equals(s)) {
					String fileServiceLyS = path + "\\src\\test\\java\\" + classPath.replace(".", "\\") + "\\SysUserService.txt";
					log.info("--fileServiceLyS-------" + fileServiceLyS);
					File fileServiceLy = new File(fileServiceLyS);
					String serviceLy = read(fileServiceLy);
					// log.info("--serviceLy-------" + serviceLy);
					serviceLy = serviceLy.replaceAll("\\{entityName\\}", entityName);
					serviceLy = serviceLy.replaceAll("\\{entityNameXx\\}", entityNameXx);
					serviceLy = serviceLy.replaceAll("\\{entityPath\\}", entityPath);
					serviceLy = serviceLy.replaceAll("\\{mapperPath\\}", mapperPath);
					serviceLy = serviceLy.replaceAll("\\{servicePath\\}", servicePath);
					serviceLy = serviceLy.replaceAll("\\{serviceImplPath\\}", serviceImplPath);
					write(serviceLy, fileServicecj);
				} else if ("serviceImpl".equals(s)) {
					String fileServiceImplLyS = path + "\\src\\test\\java\\" + classPath.replace(".", "\\") + "\\SysUserServiceImpl.txt";
					log.info("--fileServiceImplLyS-------" + fileServiceImplLyS);
					File fileServiceImplLy = new File(fileServiceImplLyS);
					String serviceImplLy = read(fileServiceImplLy);
					// log.info("--serviceImplLy-------" + serviceImplLy);
					serviceImplLy = serviceImplLy.replaceAll("\\{entityName\\}", entityName);
					serviceImplLy = serviceImplLy.replaceAll("\\{entityNameXx\\}", entityNameXx);
					serviceImplLy = serviceImplLy.replaceAll("\\{entityPath\\}", entityPath);
					serviceImplLy = serviceImplLy.replaceAll("\\{mapperPath\\}", mapperPath);
					serviceImplLy = serviceImplLy.replaceAll("\\{servicePath\\}", servicePath);
					serviceImplLy = serviceImplLy.replaceAll("\\{serviceImplPath\\}", serviceImplPath);
					write(serviceImplLy, fileServiceImplcj);
				}
			}
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 读取文件
	 * 
	 * @param src
	 * @return
	 */
	public static String read(File src) {
		StringBuffer res = new StringBuffer();
		String line = null;
		try {
			BufferedReader reader = new BufferedReader(new FileReader(src));
			while ((line = reader.readLine()) != null) {
				res.append(line + "\n");
			}
			reader.close();
		} catch (FileNotFoundException e) {
			log.info("---read---FileNotFoundException-------" + e.getMessage(), e);
		} catch (IOException e) {
			log.info("---read---IOException-------" + e.getMessage(), e);
		}
		return res.toString();
	}

	/**
	 * 写文件
	 * 
	 * @param cont
	 * @param dist
	 * @return
	 */
	public static boolean write(String cont, File dist) {
		try {
			BufferedWriter writer = new BufferedWriter(new FileWriter(dist));
			writer.write(cont);
			writer.flush();
			writer.close();
			return true;
		} catch (IOException e) {
			log.info("---write---IOException-------" + e.getMessage(), e);
			return false;
		}
	}
}
